/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.api.mgrInterface.method;

import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.bef.component.ICompParameter;
import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrComponent;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethod;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethodParameter;
import com.inspur.edp.bef.dtgenerator.BEGeneratorContext;
import com.inspur.edp.bef.dtgenerator.DotNetToJavaStringHelper;
import com.inspur.edp.bef.dtgenerator.api.BefApiGeneratorContext;
import com.inspur.edp.caf.generator.baseInfo.InterfaceMethodGenerateInfo;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.InterfaceMethodGenerator;
import com.inspur.edp.caf.generator.method.InterfaceMethodGeneratorContext;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;

public class MgrInterfaceMethodGenerator extends InterfaceMethodGenerator {

	// region BasicInfo
	private BizMgrAction action;
	private BefApiGeneratorContext pool;
	private BEMgrComponent compMetadata;
	private BEMgrMethod method;

	public MgrInterfaceMethodGenerator(BizMgrAction action, BefApiGeneratorContext pool) {
		this.action = action;
		this.pool = pool;
	}

	@Override
	protected InterfaceMethodGeneratorContext createClassInfoContext() {
		return new InterfaceMethodGeneratorContext();
	}

	@Override
	protected InterfaceMethodGenerateInfo createClassMethodInfo() {
		return new InterfaceMethodGenerateInfo();
	}

	@Override
	protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
		return new java.util.ArrayList<ModifierKeyword>();
	}

	@Override
	protected String getMethodName() {
		return action.getCode();
	}

	@Override
	protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
		java.util.ArrayList<ParameterInfo> result = new java.util.ArrayList<ParameterInfo>();
		if (method.getParameterCollection() == null) {
			return result;
		}
		for (Object item : method.getParameterCollection()) {
			BEMgrMethodParameter par = (BEMgrMethodParameter) item;
			result.add(com.inspur.edp.cef.generator.overall.Utils.buildParGenInfo(par, pool, null));
		}
		return result;
	}

	@Override
	protected TypeInfo getReturnType() {
		return com.inspur.edp.cef.generator.overall.Utils.getReturnType(method, pool.getBeGenCtx());
	}

	@Override
	protected void doInitialize() {
		// TODO: 联动计算校验规则也应该加上提示
		if (DotNetToJavaStringHelper.isNullOrEmpty(action.getComponentId())) {
			throw new RuntimeException(
					/*"BEGen_0004", */ "业务实体["
					+ pool.getBeGenCtx().getBE().getName()
					+ "]上的自定义动作["
					+ action.getName()
					+ "]没有对应的构件元数据, 请打开be设计器点击保存并同步.");
		}
		GspMetadata metadata =
				pool.getBeGenCtx().getMetadataPool().getMetadata(action.getComponentPkgName(), action.getComponentId());

		GspComponent comp = (GspComponent) metadata.getContent();
		if (!comp.getComponentType().equals("BEMgrComponent")) {
			throw new RuntimeException("错误的类型");
		}
		Object tempVar = comp.getMethod();
		method = (BEMgrMethod) ((tempVar instanceof BEMgrMethod) ? tempVar : null);
		checkBEMgrMethod(method);
	}

	private void checkBEMgrMethod(BEMgrMethod method) {
		for (Object para : method.getParameterCollection()) {
			checkPara((ICompParameter)para);
		}
		checkReturnValue(method.getReturnValue());
	}

	private void checkPara(ICompParameter para) {
//		throw new RuntimeException(
//				/*"BEGen_0004", */ "业务实体["
//				+ pool.getBE().getName()
//				+ "]上的自定义动作["
//				+ action.getName()
//				+ "]的参数["
//				+ para.getParamName()
//				+ "]的Java类名为空,请完善.");
	}

	private void checkReturnValue(ICompParameter returnValue) {
//		throw new RuntimeException(
//				/*"BEGen_0004", */ "业务实体["
//				+ pool.getBE().getName()
//				+ "]上的自定义动作["
//				+ action.getName()
//				+ "]返回值Java类名为空,请完善.");
	}
	// endregion
}
